#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <list>
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_CommHdr.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_Types.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_ProtocolHdr.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine/NetEngine_Core/NetCore_Define.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine/NetEngine_Core/NetCore_Error.h"

//g++ -std=gnu++11 -Wall -g TestS_UDXService.cpp -o TestS_UDXService -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Core -lNetEngine_BaseLib -lNetEngine_Core -Wl,-rpath=../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Core,--disable-new-dtags

TCHAR tszClientAddr[32];

//客户进入
void __stdcall NetCore_UDX_CBLogin(LPCSTR lpszClientAddr, LPVOID lParam)
{
    printf("有用户连接:%s\n", lpszClientAddr);
    _tcscpy_s(tszClientAddr, sizeof(tszClientAddr), lpszClientAddr);
}
//客户离开
void __stdcall NetCore_UDX_CBLeave(LPCSTR lpszClientAddr, LPVOID lParam)
{
    printf("有用户离开:%s\n", lpszClientAddr);
}
int main()
{
    XNETHANDLE xhNet;
    NETCORE_UDXCONFIG st_UDXConfig;
    memset(&st_UDXConfig,'\0',sizeof(NETCORE_UDXCONFIG));
    LPCTSTR lpszFile = _T("./1.png");
    FILE *pSt_File = _tfopen(lpszFile, "wb");
    if (NULL == pSt_File)
    {
        printf(_T("创建文件失败"));
        return 0;
    }

    st_UDXConfig.bEnableLogin = TRUE;
    st_UDXConfig.bEnableReorder = TRUE;
    st_UDXConfig.bEnableRryTime = TRUE;
    st_UDXConfig.nWindowSize = 1000000;

    memset(tszClientAddr, '\0', sizeof(tszClientAddr));

    if (!NetCore_UDXSocket_InitEx(&xhNet, &st_UDXConfig, 1339))
    {
        printf("启动UDX服务失败");
        return -1;
    }
    NetCore_UDXSocket_CBSetEx(xhNet, NetCore_UDX_CBLogin, NetCore_UDX_CBLeave);

    int i = 0;
    int nCount = 0;
    while (1)
    {
        int nMsgLen = 2048;
        TCHAR tszMsgBuffer[2048];
        memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
        if (NetCore_UDXSocket_RecvEx(xhNet, tszClientAddr, tszMsgBuffer, &nMsgLen))
        {
            nCount += nMsgLen;
            fwrite(tszMsgBuffer, 1, nMsgLen, pSt_File);
            //NetCore_UDXSocket_SendEx(xhNet, tszClientAddr, "abc", 3);
            printf("%d=%d - %d\n", i, nMsgLen,nCount);
        }
        if (nCount >= 261223)
        {
            break;
        }

        i++;
        Sleep(1);
    }
    fclose(pSt_File);
    return 0;
}
